﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Library;
using QLKH.PopUp;

namespace QLKH.PopUp.taodulieu
{
    public partial class SuaChiSoCu : Form
    {
        private SqlConnection sqlcon;
        string tbsudung = UpdateData.tbSudungnew;
        public SuaChiSoCu()
        {
            InitializeComponent();
        }

        private void SuaChiSoCu_Load(object sender, EventArgs e)
        {
            clKhachhang clkh = new clKhachhang();
            AutoCompleteStringCollection dskh = new AutoCompleteStringCollection();

            DataTable dtds = clkh.getautocomplete(0, 0);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows)
                {
                    dskh.Add(row["maCu"].ToString());
                }
            }

            txtMaKH.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtMaKH.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMaKH.AutoCompleteCustomSource = dskh;
        }

        private void btntinhcuoc_Click(object sender, EventArgs e)
        {
            clTaoTbSuDung tbs = new clTaoTbSuDung();
            string makh = txtMaKH.Text.Trim();


            if (txtchisocumoi.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa nhập chỉ số cũ mới", "Thông báo");
            }
            else
            {
                int cscmoi = int.Parse(txtchisocumoi.Text.Trim());

                Int64 idkh = 0;
                Int16 somoi = 0;

                if (makh != "")
                {
                    try
                    {
                        using (sqlcon = clHoadon.GetConnection())
                        {
                            sqlcon.Open();

                            SqlCommand command = new SqlCommand("select o.id,somoi2 from tbKhachHang o join " + tbsudung + " b on b.idkh=o.id where maCu=N'" + makh + "' and trangthai=3", sqlcon);
                            SqlDataReader reader = command.ExecuteReader();
                            //datagridTBSuDung.Rows[int.Parse(datagridTBSuDung.CurrentRow.Index.ToString())].Cells["tongtien"].Value = int.Parse(datagridTBSuDung.Rows[int.Parse(datagridTBSuDung.CurrentRow.Index.ToString())].Cells["idkh"].Value.ToString());
                            while (reader.Read())
                            {
                                idkh = Int64.Parse(reader["id"].ToString());
                                //Sửa chỉ số cũ cho những khách hàng đã tính cước
                                somoi = Int16.Parse((reader["somoi2"].ToString() == "") ? "0" : reader["somoi2"].ToString());
                            }
                            sqlcon.Close();
                        }
                    }
                    catch { }
                    finally { sqlcon.Dispose(); }

                    try
                    {
                        using (sqlcon = clHoadon.GetConnection())
                        {
                            sqlcon.Open();
                            SqlCommand command = new SqlCommand("update " + tbsudung + " set socu2=" + cscmoi + " where idkh=" + idkh + "", sqlcon);
                            SqlDataReader reader = command.ExecuteReader();

                            tbs.tinhcuoc(idkh, somoi, tbsudung);

                            sqlcon.Close();
                        }
                    }
                    catch { }
                    finally { sqlcon.Dispose(); }

                    MessageBox.Show("Đã cập nhật xong chỉ số cũ khách hàng " + makh);
                    txtMaKH.Text = "";
                    txtchisocutruoc.Text = "";
                    txtchisocumoi.Text = "";
                    txtMaKH.Focus();

                    this.Close();
                }
                else
                {
                    MessageBox.Show("Mã khách hàng này không tồn tại!","Thông Báo");
                    txtMaKH.Text = "";
                    txtchisocutruoc.Text = "";
                    txtchisocumoi.Text = "";
                    txtMaKH.Focus();
                }
            }
        }

        private void txtMaKH_TextChanged(object sender, EventArgs e)
        {
            int i;
            string makh = txtMaKH.Text.Trim();

            try
            {
                using (sqlcon = clHoadon.GetConnection())
                {
                    sqlcon.Open();
                    SqlCommand command = new SqlCommand("select o.id,socu2,somoi2,maCu from tbKhachHang o join " + tbsudung + " b on b.idkh=o.id", sqlcon);
                    SqlDataAdapter reader = new SqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    reader.Fill(dt);

                    for (i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        if (dr["maCu"].ToString() == makh)
                        {
                            txtchisocutruoc.Text = dr["socu2"].ToString();
                        }
                    }

                    sqlcon.Close();
                }
            }
            catch { }
            finally { sqlcon.Dispose(); }
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
